//
//  MujiDubButton.m
//  testdemo
//
//  Created by yule on 2017/6/23.
//  Copyright © 2017年 yule. All rights reserved.
//

#import "MujiDubButton.h"

@interface MujiDubButton ()

@property (nonatomic, strong) UILabel   *selectedBackView;


@end

@implementation MujiDubButton

+(instancetype)buttonWithFrame:(CGRect)frame title:(NSString *)title image:(UIImage *)image
{
    MujiDubButton *btn = [[MujiDubButton alloc] initWithFrame:frame];
    btn.title = title;
    btn.image = image;
    
    return btn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        //[self addSubview:_imageView];
        
        _selectedBackView = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width, 21)];
        [self addSubview:_selectedBackView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width, 21)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:_titleLabel];
        
        // ImageView的约束，让他随着父视图的边框变化而变化
        //        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(self.mas_left);
        //            make.right.equalTo(self.mas_right).offset(1.0f/kScreenScale);
        //            make.top.equalTo(self.mas_top);
        //            make.bottom.equalTo(self.mas_bottom);
        //        }];
        //
        //        // titleLabel的高度
        //        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(self);
        //            make.right.equalTo(self);
        //            make.bottom.equalTo(self);
        //            make.height.equalTo(@21);
        //        }];
    }
    return self;
}

#pragma mark - Setter & Getter
- (void)setImage:(UIImage *)image
{
    if (_image != image) {
        _image = image;
        if (!self.selected) {
            self.imageView.image = image;
        }
    }
}
- (void)setTitle:(NSString *)title
{
    if (_title != title) {
        _title = title;
        
        self.titleLabel.text = title;
        
        NSString *firstWord = [self.titleLabel.text substringWithRange:NSMakeRange(0, 1)];
        if ([firstWord isEqualToString:@" "]) {
            self.selectedBackView.frame = CGRectMake(3.6, self.selectedBackView.frame.origin.y, self.selectedBackView.frame.size.width - 3.6, self.selectedBackView.frame.size.height);
        }
        
    }
}

- (void)setTextFont:(UIFont *)textFont
{
    self.titleLabel.font = textFont;
}
- (void)setTextColor:(UIColor *)textColor
{
    self.titleLabel.textColor = textColor;
}

-(void)setSelectedCloror:(UIColor *)color
{
    self.selectedBackView.backgroundColor = color;
}
- (void)layoutSubviews
{
    
    NSLog(@"%@", NSStringFromCGRect(self.frame));
    
    [self.titleLabel setFrame:CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width, 21)];
    
    NSString *firstWord = [self.titleLabel.text substringWithRange:NSMakeRange(0, 1)];
    if ([firstWord isEqualToString:@" "]) {
        self.selectedBackView.frame = CGRectMake(3.6, self.selectedBackView.frame.origin.y, self.selectedBackView.frame.size.width - 3.6, self.selectedBackView.frame.size.height);
        
        [self.selectedBackView setFrame:CGRectMake(3.6, (self.bounds.size.height-21)/2, self.bounds.size.width - 3.6, 21)];
        
    }else {
        [self.selectedBackView setFrame:CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width, 21)];
        
    }
    NSLog(@"----------  layoutSubviews");
}

@end
